package com.xsmart.oa.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by lin.wenhui on 2018/1/9.
 */
@Component
public interface QueryAdminMenuDAO {

    /**
     * 查询该用户所拥有菜单权限
     * @param userId
     * @param pId
     * @return
     */
    @Select("<script>select menu_id, pmenu_id, menu_name as name, menu_icon as icon,menu_code as path, menu_url as component from cfgs_menu where status=1 and menu_code in\n" +
            "(select action_code from cfgs_action where ACTION_ID in\n" +
            "(select action_id from infu_admin_action where user_id=\n" +
            "(select user_id from infs_admin_user a where  a.user_id=#{userId})))" +
            "and pmenu_id=#{pId} order by menu_sort</script>")
    List<Map<String, Object>> queryAdminMenu(@Param("userId") String userId, @Param("pId") Integer pId);

    /**表关联查询管理员菜单
     *
     * @param userId
     * @param pId
     * @return
     */
    @Select("<script>" +
            "select menu_id, pmenu_id, menu_name as name, menu_icon as icon,menu_code as path, menu_url as component " +
            "from cfgs_menu m where status=1 and pmenu_id=#{pId} " +
            "and exists(select * from cfgs_action c where m.menu_code=c.ACTION_CODE " +
            "and exists(select * from infu_admin_action a where a.user_id=#{userId} and a.ACTION_ID=c.ACTION_ID))" +
            "</script>")
    List<Map<String, Object>> queryMenu(@Param("userId") String userId, @Param("pId") Integer pId);



    @Select("<script>select menu_id, pmenu_id, menu_name as name, menu_icon as icon,menu_code as path, menu_url as component from cfgs_menu where status=1 and menu_code in\n" +
            "(select action_code from cfgs_action where ACTION_ID in\n" +
            "(select action_id from infu_admin_action where user_id=\n" +
            "(select user_id from infs_admin_user a where  a.user_id=#{userId}) ))" + " order by menu_sort</script>")
    List<Map<String, Object>> queryAdminMenu2(@Param("userId") String userId, @Param("pId") Integer pId);

    /**
     * 获取所有的菜单
     * @param pId
     * @return
     */
    @Select("<script>select menu_id, pmenu_id, menu_name as name, menu_icon as icon,menu_code as path, menu_url as component from cfgs_menu where status=1 and pmenu_id=#{pId} order by menu_sort</script>")
    List<Map<String, Object>> queryAllMenu(@Param("pId")Integer pId);

    /**
     * 查询管理员按钮的权限
     * @param userId
     * @param actionCode
     * @return
     */
    @Select("<script>select count(*) from (select * from  (select ACTION_CODE from cfgs_action where action_id " +
            "in(select ACTION_ID from infu_admin_action where user_id=#{userId})) t where t.action_code =#{actionCode}) a</script>")
    int queryAdminButtonPower(@Param("userId") String userId, @Param("actionCode") String actionCode);
}
